package com.cskaoyan.market.service.wx;/*
 *文件名: WxCollectServiceImpl
 *创建者:wxin
 *创建时间:2024/5/20 21:07
 */

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketCollectMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.vo.CollectVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    SecurityManager securityManager;

    @Override
    public Integer getCollectNumberByGoodsId(Integer id) {
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        criteria.andValueIdEqualTo(id);
        criteria.andTypeEqualTo((byte) 0);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExampleSelective(marketCollectExample, MarketCollect.Column.userId);
        return marketCollects.size();
    }

    @Override
    public List<CollectVo> list(Integer type, Integer page, Integer limit) {

        MarketCollectExample marketCollectExample=new MarketCollectExample();
        MarketCollectExample.Criteria criteria1 = marketCollectExample.createCriteria();
        criteria1.andDeletedEqualTo(false);
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        List<CollectVo> marketGoodsCollects=new ArrayList<>();
        for (int i = 0; i <marketCollects.size() ; i++) {
            MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
            criteria.andIdEqualTo(marketCollects.get(i).getValueId());
            MarketGoods marketGoods=marketGoodsMapper.selectOneByExample(marketGoodsExample);
            CollectVo collectVo=new CollectVo();
            collectVo.setBrief(marketGoods.getBrief());
            collectVo.setId(marketCollects.get(i).getId());
            collectVo.setName(marketGoods.getName());
            collectVo.setPicUrl(marketGoods.getPicUrl());
            collectVo.setRetailPrice(marketGoods.getRetailPrice());
            collectVo.setType(Byte.valueOf("0"));
            collectVo.setValueId(marketGoods.getGoodsSn());
            marketGoodsCollects.add(collectVo);
        }

        return marketGoodsCollects;
    }

    @Override
    public int deleteById(Integer id) {

        return marketCollectMapper.logicalDeleteByPrimaryKey(id);
    }

    @Override
    public int addOrDelete(Integer id,Integer type) {
        MarketCollectExample collectExample=new MarketCollectExample();
        MarketCollectExample.Criteria criteria = collectExample.createCriteria();
        if(id!=null){
            criteria.andValueIdEqualTo(id);
        }
        MarketCollect marketCollect= marketCollectMapper.selectOneByExample(collectExample);
        if(marketCollect!=null) {
            if (marketCollect.getDeleted()) {
                //说明被删除了
                marketCollect.setDeleted(false);
                marketCollect.setUpdateTime(LocalDateTime.now());
                return marketCollectMapper.updateByPrimaryKey(marketCollect);
            } else {
                //说明未删除
                marketCollect.setDeleted(true );
                marketCollect.setUpdateTime(LocalDateTime.now());
                return marketCollectMapper.updateByPrimaryKey(marketCollect);
            }
        }else{
            MarketGoodsExample marketGoodsExample=new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria1 = marketGoodsExample.createCriteria();
            criteria1.andIdEqualTo(id);
            MarketGoods marketGoods=marketGoodsMapper.selectOneByExample(marketGoodsExample);
            if(marketGoods!=null){
                MarketCollect marketCollect1=new MarketCollect();
                SecurityUtils.setSecurityManager(securityManager);
                Subject subject = SecurityUtils.getSubject();
                Session session = subject.getSession();
                MarketUser user = (MarketUser) session.getAttribute("user");
                marketCollect1.setUserId(user.getId());
                marketCollect1.setAddTime(LocalDateTime.now());
                marketCollect1.setValueId(marketGoods.getId());
                marketCollect1.setType(Byte.valueOf("0"));
                marketCollect1.setDeleted(false);
                marketCollect1.setUpdateTime(LocalDateTime.now());
                return marketCollectMapper.insertSelective(marketCollect1);

            }
            return -1;
        }
    }

    @Override
    public int add(MarketCollect collect) {
        collect.setAddTime(LocalDateTime.now());
        collect.setUpdateTime(LocalDateTime.now());
        return marketCollectMapper.insertSelective(collect);
    }

}
